<?php

/**
 * Impress - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Impress
 * @package Impress
 */

/**
 * This class transmits requested stuff to client.
 *
 * @package Impress
 * @subpackage Core
 */

class ADS_Transmitter{
    
    public function sendScheme($format = 'xml'){ 
        /*$scheme = new SimpleXMLElement("<?xml version='1.0' ?><scheme></scheme>");
        $el['frames'] = $scheme->addChild('frames');
        //layout frames
        $el['layouts'] = $el['frames']->addChild('layouts');
        $lfModel = new ADS_LayoutFrameModel();        
        foreach($lfModel->getFrames() as $frame){
            $el['frame'] = $el['layouts']->addChild('frame');
            $el['frame']->addAttribute('id', 'lf'.$frame->frameId);
            $el['frame']->addAttribute('url', $frame->guid);
        }
        //data frames
        $el['data'] = $el['frames']->addChild('data');
        $dfModel  = new ADS_DataFrameModel();
        $dfiModel = new ADS_DataFrameItemModel();
        foreach($dfModel->getFrames() as $frame){
            $el['frame'] = $el['data']->addChild('frame');
            $el['frame']->addAttribute('id', 'df'.$frame->frameId);
            $el['frame']->addAttribute('htmlId', $frame->htmlId);
            $el['frame']->addChild('name', $frame->name);
            //data sources
            $el['timeline'] = $el['frame']->addChild('timeline');
            foreach($dfiModel->getItems((int)$frame->frameId) as $source) {
                $el['source'] = $el['timeline']->addChild('source');
                $el['source']->addAttribute('id', 'ds'.$source->sourceId);
            }
        }
        //data sources
        $el['datasources'] = $scheme->addChild('datasources');
        $dsModel = new ADS_DataSourceModel();
        $dsiModel = new ADS_DataSourceItemModel();
        foreach($dsModel->getSources() as $source){
            $el['source'] = $el['datasources']->addChild('source');
            $el['source']->addAttribute('id', 'ds'.$source->sourceId);
            $el['source']->addAttribute('content', $source->type);
            //items
            $el['timeline'] = $el['source']->addChild('timeline');
            foreach($dsiModel->getItems((int)$source->sourceId) as $item) {
                $el['item'] = $el['timeline']->addChild('item');
                $el['item']->addAttribute('url', $item->guid);
            }
        }
        //send output
        header("Content-Type:text/xml");  
        print $scheme->asXML();*/
        $scheme = array(
            'frames' => array(
                'layouts' => array(),
                'data'    => array(),
            ),
            'datasources' => array(),
        );
        $lfModel = new ADS_LayoutFrameModel();        
        foreach($lfModel->getFrames() as $frame){
            $scheme['frames']['layouts'][] = array(
                'id'  => 'lf'.$frame->frameId,
                'url' => $frame->guid
            );
        }
        
        //data frames
        $dfModel  = new ADS_DataFrameModel();
        $dfiModel = new ADS_DataFrameItemModel();
        foreach($dfModel->getFrames() as $frame){
            $dataframe = array(
                'id'       => 'lf'.$frame->frameId,
                'htmlId'   => $frame->htmlId,
                'name'     => $frame->name,
                'timeline' => array()
            );            
            //data sources
            foreach($dfiModel->setSorter(array('position' => 'ASC'))->getItems((int)$frame->frameId) as $source) {
                $dataframe['timeline'][] = array(
                    'id' => 'ds'.$source->sourceId
                );
            }
            $scheme['frames']['data'][] = $dataframe;
        }
        //data sources
        $dsModel = new ADS_DataSourceModel();        
        $wpModel = new ADS_WPModel();        
        foreach($dsModel->getSources() as $source){
            $datasource = array(
                'id'       => 'ds'.$source->sourceId,
                'content'  => $source->content,
                'timeline' => array()
            );  
            $dsiModel = new ADS_DataSourceItemModel($source->content,$source->type);
            //items
            foreach($dsiModel->getItems((int)$source->sourceId) as $item) {
                switch($source->type){                    
                    case 'item':
                        $datasource['timeline'][] = array(
                            'url'       => $item->guid,
                            'duration'  => $item->duration*1000,
                            'validFrom' => $item->validFrom,
                            'validTo'   => $item->validTo
                        );
                    break;
                    
                    case 'tag':
                        foreach($wpModel->getPostsWithTag($source->content, (int)$item->targetId) as $wpItem){
                            $datasource['timeline'][] = array(
                                'url'       => $wpItem->guid,
                                'duration'  => $item->duration*1000,
                                'validFrom' => $item->validFrom,
                                'validTo'   => $item->validTo
                            );
                        }
                    break;
                }                
            }
            $scheme['datasources'][] = $datasource;
        }
        //send output
        //header('Content-type: application/json');  
        print json_encode($scheme);
        
    }
    
}
